package org.hackystat.sdsa.analyzer.model.activity.edit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.logging.Logger;

import javax.xml.datatype.XMLGregorianCalendar;

import jess.Fact;

import org.hackystat.sdsa.analyzer.model.activity.ActivityTestBase;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Test;


/**
 * Tests unit test edit action. 
 * 
 * @author Hongbing Kou
 */
public class TestUnitTestEditActivity extends ActivityTestBase  {
  /** Sensor data. */
  private SensorData sensorData;
  private Logger logger;
  
  /**
   * Prepares test variables.
   * @throws Exception If error in testing.
   */
  @Override public void setUp() throws Exception {
    super.setUp();
    this.logger = Logger.getLogger(TestUnitTestEditActivity.class.getName());

    this.sensorData = new SensorData();
    XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2005-01-01T08:30:45");
    sensorData.setTimestamp(tstamp);
    sensorData.setResource("file:/C:/cvs/work/com/foo/TestBar.java");
    sensorData.setSensorDataType("DevEvent");

    Properties props = new Properties();
    sensorData.setProperties(props);
    
    Property type = new Property();
    type.setKey("Type");
    type.setValue("Edit");
    props.getProperty().add(type);
    
    Property subType = new Property();
    subType.setKey("Subtype");
    subType.setValue("StateChange");
    props.getProperty().add(subType);
    
    Property currentSize = new Property();
    currentSize.setKey(EditActivity.CURRENT_SIZE);
    currentSize.setValue("269");
    props.getProperty().add(currentSize);

    Property className = new Property();
    className.setKey(ProductionEditActivity.CLASS_NAME);
    className.setValue("com.foo.TestBar");
    props.getProperty().add(className);

    Property currentStatements = new Property();
    currentStatements.setKey(ProductionEditActivity.CURRENT_STATEMENTS);
    currentStatements.setValue("130");
    props.getProperty().add(currentStatements);

    Property currentMethods = new Property();
    currentMethods.setKey(ProductionEditActivity.CURRENT_METHODS);
    currentMethods.setValue("6");
    props.getProperty().add(currentMethods);

    Property currentTestMethods = new Property();
    currentTestMethods.setKey(UnitTestEditActivity.CURRENT_TEST_METHODS);
    currentTestMethods.setValue("5");
    props.getProperty().add(currentTestMethods);

    Property currentTestAssertions = new Property();
    currentTestAssertions.setKey(UnitTestEditActivity.CURRENT_TEST_ASSERTIONS);
    currentTestAssertions.setValue("32");
    props.getProperty().add(currentTestAssertions);
  }
  
  /**
   * Tests unit test edit action. 
   * 
   * @throws Exception Error while testing.
   */
  @Test public void testUnitTestEdit() throws Exception {
    UnitTestEditActivity activity = new UnitTestEditActivity(this.sensorData, this.logger);
  
    assertEquals("Test number of test methods", 5, activity.getNumOfTestMethods());
    assertEquals("Test number of test methods increase", 0, 
        activity.getTestMethodIncrease());
    
    assertEquals("Test number of test assertions", 32, activity.getNumOfTestAssertions());
    assertEquals("Test number of test assertion increase", 0, 
        activity.getTestAssertionIncrease());
    
    assertEquals("Test number of methods", 6, activity.getNumOfMethods());
    assertEquals("Test method increase", 0, activity.getMethodIncrease());
    
    assertEquals("Test number of statements", 130, activity.getNumOfStatements());
    assertEquals("Test statement increase", 0, activity.getStatementIncrease());

    assertEquals("Test file size ", 269, activity.getFileSize());
    assertEquals("Test file size increase", 0, activity.getFileSizeIncrease());
  }
  
  /**
   * Tests conversion from action object to JESS fact.
   * 
   * @throws Exception If error while converting to Jess fact.
   */
  @Override public void testJessFactConversion() throws Exception {
    UnitTestEditActivity activity = new UnitTestEditActivity(this.sensorData, this.logger);
    activity.setDuration(189);
    activity.setFileSizeIncrease(10);
    activity.setMethodIncrease(1);
    Fact assertedFact = activity.assertAsFact(1, engine);
    assertTrue("Test action is asserted to working memory", assertedFact.getFactId() >= 0);
  }  
}
